import { Body, Controller, Post, Inject } from '@midwayjs/core';
import { ApiResponse, ApiTags } from '@midwayjs/swagger';
import {
  FlagResDto,
  Pagination,
  RepairAddItem,
  RepairEditItems,
  RepairId,
  RepairItems,
  RepairItemsResDto,
  RepairsUpdateDto,
} from '../model';
import { ServerResponse } from '../util/response';
import { RepairService } from '../service/repair.service';
import { Context } from '@midwayjs/koa';

@ApiTags(['报修'])
@Controller('/repair')
export class RepairController {
  @Inject()
  ctx: Context;
  @Inject()
  repairService: RepairService;

  @Post('/list', { summary: '报修列表' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: RepairItemsResDto,
  })
  async list(@Body() req: Pagination) {
    const params: Pagination = {
      pageNo: 1,
      pageSize: 10,
      ...req,
    };
    const res = await this.repairService.getList(params);
    return ServerResponse.JSON_TPL({
      list: res.rows,
      pagination: {
        ...params,
        total: res.count,
      },
    });
  }

  @Post('/listByPerson', { summary: '个人房屋列表' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: RepairItemsResDto,
  })
  async listByPerson(@Body() req: Pagination) {
    const params: Pagination = {
      pageNo: 1,
      pageSize: 10,
      ...req,
    };
    const res = await this.repairService.listByPerson(
      params,
      this.ctx.state.user.id
    );
    return ServerResponse.JSON_TPL({
      list: res.rows,
      pagination: {
        ...params,
        total: res.count,
      },
    });
  }

  @Post('/add', { summary: '添加报修' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async add(@Body() req: RepairAddItem) {
    const res = await this.repairService.addItem({
      ...req,
      status:'1'
    }, this.ctx.state.user.id);
    return ServerResponse.JSON_TPL({
      flag: Object.keys(res).length > 0,
    });
  }
  @Post('/edit', { summary: '编辑报修' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async edit(@Body() req: RepairEditItems) {
    const res = await this.repairService.editItem(req);
    if (Boolean(res[0])) {
      return ServerResponse.JSON_TPL({
        flag: Boolean(res[0]),
      });
    }
    return ServerResponse.JSON_TPL(
      {
        flag: Boolean(res[0]),
      },
      Boolean(res[0]),
      '不存在该报修'
    );
  }

  @Post('/del', { summary: '删除报修' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async del(@Body() req: RepairId) {
    const res = await this.repairService.delItem(req);
    if (Boolean(res[0])) {
      return ServerResponse.JSON_TPL({
        flag: Boolean(res[0]),
      });
    }
    return ServerResponse.JSON_TPL(
      {
        flag: Boolean(res[0]),
      },
      Boolean(res[0]),
      '不存在该报修'
    );
  }

  @Post('/byId', { summary: '报修详情' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: RepairItems,
  })
  async byId(@Body() req: RepairId) {
    const res = await this.repairService.byIdItem(req);
    return ServerResponse.JSON_TPL({
      data: res,
    });
  }

  @Post('/updateStatus', { summary: '更新状态' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async updateStatus(@Body() req: RepairsUpdateDto) {
    const res = await this.repairService.updateStatus(req)
    if (Boolean(res[0])) {
      return ServerResponse.JSON_TPL({
        flag: Boolean(res[0]),
      });
    }
    return ServerResponse.JSON_TPL(
      {
        flag: Boolean(res[0]),
      },
      Boolean(res[0]),
      '不存在该报修'
    );
  }
}
